# coding: utf8


def gen():
    """可创建无限迭代生成器的生成器函数"""
    start = 0
    while True:
        yield start if start < 2 else 'too big'
        start += 1


def gen_break():
    """创建有限迭代生成器的生成器函数"""
    start = 0
    while True:
        yield start if start < 2 else 'too big'
        start += 1
        if start >= 2:
            break


def test_gen():

    g1 = gen()
    print(
        ""
        " >>> g1 = gen()\n"
        " >>> next(g1)\n"
        f"{next(g1)}\n"
        " >>> next(g1)\n"
        f"{next(g1)}\n"
    )

    g2 = gen_break()
    print(
        ""
        " >>> g2 = gen_break()\n"
        " >>> next(g2)\n"
        f"{next(g2)}\n"
        " >>> next(g2)\n"
        f"{next(g2)}\n"
        " >>> next(g2)\n"
        # f"{next(g2)}\n"
        " StopIteration\n"
    )

    g3 = gen_break()
    print(
        " >>> g3 = gen_break()\n"
        f"{[p for p in dir(g3)]}\n"
        f"{g3.gi_code}\n"
        " >>> [x for x in g3]\n"
        f"{[x for x in g3]}"
    )
